package com.mf.elephant.web.basic;

import com.mf.elephant.domain.basic.template.Template;
import com.mf.elephant.service.basic.TemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板管理Controller
 * User: zhaoming
 * Date: 2016-07-05
 * To change this template use File | Settings | File Templates.
 **/
@Controller
@RequestMapping("/web/template")
public class TemplateController {

    public Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private TemplateService templateService;

    /**
     * 初始化
     * @param request 请求对象
     * @param response 响应对象
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("basic/template/template_index");
        return modelAndView;
    }

    /**
     * 根据条件查询数据类型映射列表
     * @return 数据类型映射列表
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> selectAll() {

        List<Template> templates = templateService.selectAll();
        Map<String, Object> result = new HashMap<>();
        result.put("root", templates);
        return result;
    }

    /**
     * 详情展示
     * @return 返回页面
     */
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public ModelAndView show(Template template) {
        ModelAndView modelAndView = new ModelAndView("basic/template/template_show");
        modelAndView.addObject("type", template.getFileName().split("\\.")[1]);
        modelAndView.addObject("context", templateService.show(template));
        return modelAndView;
    }


}
